//
//  CJExten+Codable.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-11.
//

import Foundation

public extension Encodable {
    
    /// codable转Data
    func cj_codableToData() -> Data? {
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        if let data = try? encoder.encode(self) {
            return data
        }
        return nil
    }
    
    /// codable转Dict
    func cj_codableToDict() -> [String: Any]? {
        if let data = cj_codableToData(),
           let dict = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] {
            return dict
        }
        return nil
    }
    
}
